//
//  LCZNextController.m
//  PersonalNews
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 changshuhua. All rights reserved.
//

#import "LCZNextController.h"


#import "UMSocial.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define TABAR 44
#define HEADERHIHT 64


@interface LCZNextController ()<UIWebViewDelegate, UIScrollViewDelegate>
@property(nonatomic, retain)UIActivityIndicatorView *activityIndicator;
@property(nonatomic, copy)NSString *strURL;
@property(nonatomic, copy)NSString *bodyStr;
@property(nonatomic, retain)NSMutableArray *picArr;
@property(nonatomic, retain)NSMutableArray *readArr;
@property(nonatomic, retain)UIScrollView *scrollView;
@property(nonatomic, retain)UIWebView *webView;
@property(nonatomic, retain)UIView *headerView;

@property (nonatomic, strong)LCZBaseImageView *headImageView;

@end

@implementation LCZNextController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //    html解析
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share.png"] style:0 target:self action:@selector(shareAction)];
    self.strURL = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/%@/full.html", self.replyid];
    
    self.picArr = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    self.readArr = [NSMutableArray array];
    
    // 创建详情页的头
    self.headImageView = [[LCZBaseImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 65)];
    self.headImageView.image = [UIImage imageNamed:@"column1.png"];
    [self.view addSubview:self.headImageView];

    [self createData];
    
}
// 返回按钮
- (void)backAction:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)shareAction{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"563956fbe0f55a550f000822"
                                      shareText:self.strURL
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToEmail, UMShareToSms, UMShareToFacebook, UMShareToTwitter,nil]
                                       delegate:nil];
}

#pragma mark- webView的headerView,标题
- (void)createHeaderView{
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH * 0.0242, -WIDTH * 0.2415 + 35, WIDTH - WIDTH * 0.048301, WIDTH * 0.2415)];
    [self.webView addSubview:_headerView];
    _headerView.backgroundColor = [UIColor orangeColor];
    [self.webView sendSubviewToBack:_headerView];
    
    
}

#pragma mark- 解析数据
- (void)createData{
    [LCZNetWorkTools getdataWithString:_strURL block:^(id block) {
              
        self.bodyStr = [[block objectForKey:self.replyid] objectForKey:@"body"];
        
        ReadRecommendDetail *readDetail = [[ReadRecommendDetail alloc] init];
        [readDetail setValuesForKeysWithDictionary:[block objectForKey:self.replyid]];
        [_readArr addObject:readDetail];
        for (NSDictionary *temp in [[block objectForKey:self.replyid]objectForKey:@"img"] ) {
            ImgInformation *image =  [[ImgInformation alloc] init];
            [image setValuesForKeysWithDictionary:temp];
            [_picArr addObject:image];
        }
        
        [self createWebView];
        
    }];
    
}

- (void)createWebView{
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(WIDTH * 0.0242, WIDTH * 0.0242 + 55, WIDTH - WIDTH * 0.048301, HEIGHT - 64)];
    [self.view addSubview:_webView];
    
//    LCZBaseView *headView = [[LCZBaseView alloc]init];
//    headView.backgroundColor = [UIColor whiteColor];
//    headView.alpha = 0.2;
//    headView.frame = CGRectMake(0, 0, WIDTH, 60);
//    [self.view addSubview:headView];
    
    // 最上面的返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(15, 25, 30, 30);
    backButton.layer.cornerRadius = 15;
    
    backButton.backgroundColor = [UIColor colorWithRed:0.45 green:0.46 blue:0.46 alpha:1];
    [self.view addSubview:backButton];
    [backButton setImage:[UIImage imageNamed:@"left.png"] forState:UIControlStateNormal];
    [backButton setTintColor:[UIColor whiteColor]];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    _webView.delegate = self;
    _webView.scalesPageToFit = YES;
    
    self.webView.scrollView.bounces = NO;
    //    self.webView.scrollView.contentOffset = CGPointMake(0, 100);
    
    //    关闭webView滚动条
    for (UIView *aview in [_webView subviews]) {
        if ([aview isKindOfClass:[UIScrollView class]]) {
            [(UIScrollView *)aview setShowsVerticalScrollIndicator:NO];
        }
    }
    
    // 小菊花
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, WIDTH * 0.0242, WIDTH * 0.12077, WIDTH * 0.12077)];
    self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    self.activityIndicator.center = self.view.center;
    [self.view addSubview:self.activityIndicator];
    [self.activityIndicator startAnimating];
      
    if (_read.ptime == nil) {
        _read.ptime = @"";
    }
    
    NSString *head = [NSString stringWithFormat:@"<p><b><b><font size = 50>%@</b></b></p><p><font size = 7><font color=#7F7F7F>%@   <font color = #7171C6> %@</p><font color = #080808>", _read.title, _read.ptime, _read.source];
    _bodyStr = [head stringByAppendingString:_bodyStr];
   
    NSRange range = [_bodyStr rangeOfString:@"<p>"];
    _bodyStr = [_bodyStr stringByReplacingCharactersInRange:range withString:@"<p><font size = 7>"];
    for (ImgInformation * image in _picArr) {
        NSRange range1 = [_bodyStr rangeOfString:image.ref];
        if (image.pixel != nil) {
            
            NSArray *arr = [image.pixel componentsSeparatedByString:@"*"];
            CGFloat h = [arr[1] floatValue];
            CGFloat w = [arr[0] floatValue];
            
            // 960是webview固定的
            NSString *width = [NSString stringWithFormat:@"960 - 20"];
            NSString *height = [NSString stringWithFormat:@"%g", h / w * (960 - 20)];
            _bodyStr = [_bodyStr stringByReplacingCharactersInRange:range1 withString:[NSString stringWithFormat:@"<p><font size = 8> <br><img src=%@ width=%@ height=%@ ><br>", image.src, width , height]];
        }
    }
    [_webView loadHTMLString:_bodyStr baseURL:nil];
    
     
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [self.activityIndicator stopAnimating];
    [self.activityIndicator removeFromSuperview];
        
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
